<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="README" />
	<meta name="description" content="README" />
	<!-- 网页标签标题 -->
	<title>README</title>
	<link rel="shortcut icon" href="/img/docsite.ico"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/zh-cn/index.html"><img class="logo" src="/pic/icon_name_black.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">En</span><div class="header-menu"><img class="header-menu-toggle" src="/img/system/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/zh-cn/index.html" target="_self">首页</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/zh-cn/docs/README.html" target="_self">文档</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/system/docs.png" class="front-img"/><span>文档</span><img src="/img/system/docs.png" class="back-img"/></div></div><section class="content-section"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>使用文档</span><ul><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/README.html" target="_self">介绍</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/FASTSTART.html" target="_self">快速开始</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/update.html" target="_self">版本升级</a></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><a href="/zh-cn/docs/HELP.html" target="_self">后台使用指南</a></li></ul></li></ul></div><div class="doc-content markdown-body"><h2 id="plumelog-trace-%E6%8F%90%E4%BE%9B%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97">plumelog-trace 提供链路日志 <a class="header-anchor" href="#plumelog-trace-%E6%8F%90%E4%BE%9B%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97">#</a></h2>
<h4 id="%E6%9C%AC%E6%A8%A1%E5%9D%97%E5%8E%9F%E7%90%86%E6%98%AF%E5%88%A9%E7%94%A8springaop%E5%88%87%E9%9D%A2%E4%BA%A7%E7%94%9F%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97%EF%BC%8C%E6%A0%B8%E5%BF%83%E6%98%AF%E9%85%8D%E7%BD%AEspringaop%2C%E9%85%8D%E7%BD%AE%E4%B9%8B%E5%89%8D%E4%B8%8D%E7%86%9F%E6%82%89springaop%E7%9A%84%E5%BB%BA%E8%AE%AE%E5%85%88%E7%86%9F%E6%82%89%E4%B8%8B">本模块原理是利用springAOP切面产生链路日志，核心是配置springAOP,配置之前不熟悉springAOP的建议先熟悉下 <a class="header-anchor" href="#%E6%9C%AC%E6%A8%A1%E5%9D%97%E5%8E%9F%E7%90%86%E6%98%AF%E5%88%A9%E7%94%A8springaop%E5%88%87%E9%9D%A2%E4%BA%A7%E7%94%9F%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97%EF%BC%8C%E6%A0%B8%E5%BF%83%E6%98%AF%E9%85%8D%E7%BD%AEspringaop%2C%E9%85%8D%E7%BD%AE%E4%B9%8B%E5%89%8D%E4%B8%8D%E7%86%9F%E6%82%89springaop%E7%9A%84%E5%BB%BA%E8%AE%AE%E5%85%88%E7%86%9F%E6%82%89%E4%B8%8B">#</a></h4>
<h4 id="%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%EF%BC%8C%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AA%E6%A8%A1%E5%9D%97%E4%BC%9A%E4%BA%A7%E7%94%9F%E5%A4%A7%E9%87%8F%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97%EF%BC%8C%E5%B9%B6%E5%8F%91%E9%AB%98%E7%9A%84%E6%A8%A1%E5%9D%97%E4%B8%8D%E8%A6%81%E8%BF%87%E5%BA%A6%E4%BD%BF%E7%94%A8%EF%BC%8C%E7%89%B9%E5%88%AB%E6%98%AF%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9">使用注意事项，链路追踪模块会产生大量链路日志，并发高的模块不要过度使用，特别是全局打点 <a class="header-anchor" href="#%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%EF%BC%8C%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AA%E6%A8%A1%E5%9D%97%E4%BC%9A%E4%BA%A7%E7%94%9F%E5%A4%A7%E9%87%8F%E9%93%BE%E8%B7%AF%E6%97%A5%E5%BF%97%EF%BC%8C%E5%B9%B6%E5%8F%91%E9%AB%98%E7%9A%84%E6%A8%A1%E5%9D%97%E4%B8%8D%E8%A6%81%E8%BF%87%E5%BA%A6%E4%BD%BF%E7%94%A8%EF%BC%8C%E7%89%B9%E5%88%AB%E6%98%AF%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9">#</a></h4>
<h4 id="%E6%89%8B%E5%8A%A8%E6%89%93%E7%82%B9%E5%92%8C%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9%E4%B8%8D%E8%83%BD%E5%90%8C%E6%97%B6%E4%BD%BF%E7%94%A8%E7%94%A8%E4%BA%86%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9%EF%BC%8C%E6%89%8B%E5%8A%A8%E7%9A%84%E4%BC%9A%E5%A4%B1%E6%95%88">手动打点和全局打点不能同时使用用了全局打点，手动的会失效 <a class="header-anchor" href="#%E6%89%8B%E5%8A%A8%E6%89%93%E7%82%B9%E5%92%8C%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9%E4%B8%8D%E8%83%BD%E5%90%8C%E6%97%B6%E4%BD%BF%E7%94%A8%E7%94%A8%E4%BA%86%E5%85%A8%E5%B1%80%E6%89%93%E7%82%B9%EF%BC%8C%E6%89%8B%E5%8A%A8%E7%9A%84%E4%BC%9A%E5%A4%B1%E6%95%88">#</a></h4>
<h4 id="%E5%A6%82%E6%9E%9C%E9%85%8D%E7%BD%AE%E4%B8%8B%E6%9D%A5%E6%B2%A1%E6%9C%89%E9%93%BE%E8%B7%AF%E6%95%B0%E6%8D%AE%EF%BC%8C%E7%AC%AC%E4%B8%80%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E6%9C%89traceid%EF%BC%8C%E7%AC%AC%E4%BA%8C%E4%BD%A0%E7%9A%84aop%E7%94%9F%E6%95%88%E4%BA%86%E5%90%97%EF%BC%9F">如果配置下来没有链路数据，第一检查是否有traceID，第二你的AOP生效了吗？ <a class="header-anchor" href="#%E5%A6%82%E6%9E%9C%E9%85%8D%E7%BD%AE%E4%B8%8B%E6%9D%A5%E6%B2%A1%E6%9C%89%E9%93%BE%E8%B7%AF%E6%95%B0%E6%8D%AE%EF%BC%8C%E7%AC%AC%E4%B8%80%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E6%9C%89traceid%EF%BC%8C%E7%AC%AC%E4%BA%8C%E4%BD%A0%E7%9A%84aop%E7%94%9F%E6%95%88%E4%BA%86%E5%90%97%EF%BC%9F">#</a></h4>
<ol>
<li>引入</li>
</ol>
<pre><code class="language-xml">            <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
               <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.plumelog<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
               <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>plumelog-trace<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
               <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ol start="2">
<li>在项目中添加扫描路径，注意：如果原来你的项目没有扫描路径，不要只加这个，也要把你自己的项目的加了，不然只扫描plumelog的路径了</li>
</ol>
<ul>
<li>注解形式</li>
</ul>
<pre><code>     @ComponentScan(&quot;com.plumelog&quot;)
</code></pre>
<ul>
<li>xml配置形式</li>
</ul>
<pre><code class="language-xml">     <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">"com.plumelog"</span>/&gt;</span>
</code></pre>
<ol start="3">
<li>需要自己的项目引入aop的 （这里默认scope 为 provided) 已经有的不要加了</li>
</ol>
<pre><code class="language-xml">            <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-aop<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.1.11.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>provided<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span>
                <span class="hljs-comment">&lt;!-- scope 为 provided 是为了不与使用者的版本冲突--&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<ol start="4">
<li>手动打点 在需要记录的方法上加入 @Trace 就可以记录链路日志了</li>
</ol>
<pre><code class="language-java">        <span class="hljs-meta">@Trace</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testLog</span><span class="hljs-params">()</span> </span>{
            easyLogDubboService.testLogDubbo();
        }
</code></pre>
<ol start="5">
<li>全局打点 需要自己定义切入点 (demo 如下 )  当定义全局打点时，手动打点就会失效</li>
</ol>
<pre><code class="language-java">    <span class="hljs-meta">@Aspect</span>
    <span class="hljs-meta">@Component</span>
    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AspectConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractAspect</span> </span>{
    
        <span class="hljs-meta">@Around</span>(<span class="hljs-string">"within(com.xxxx..*))"</span>)<span class="hljs-comment">//这边写自己的包的路径</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">around</span><span class="hljs-params">(JoinPoint joinPoint)</span> </span>{
            <span class="hljs-keyword">return</span> aroundExecute(joinPoint);
        }
    }
</code></pre>
<ol start="6">
<li>如果不想再自己的控制台或者文件输出里看到trace日志可以通过添加过滤器过滤掉,logback的例子如下</li>
</ol>
<pre><code class="language-xml">    <span class="hljs-tag">&lt;<span class="hljs-name">appender</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"CONSOLE"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>
        <span class="hljs-comment">&lt;!--此过滤器过滤掉所有的trace日志，3.4.1版本logback自带的过滤类--&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">filter</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.plumelog.logback.util.FilterSyncLogger"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">level</span>&gt;</span>info<span class="hljs-tag">&lt;/<span class="hljs-name">level</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">filterPackage</span>&gt;</span>com.plumelog.trace.aspect.AbstractAspect<span class="hljs-tag">&lt;/<span class="hljs-name">filterPackage</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">encoder</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Pattern</span>&gt;</span>${CONSOLE_LOG_PATTERN}<span class="hljs-tag">&lt;/<span class="hljs-name">Pattern</span>&gt;</span>\
            <span class="hljs-tag">&lt;<span class="hljs-name">charset</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">charset</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">encoder</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">appender</span>&gt;</span>
</code></pre>
<p>3.4之前的版本可以复制以下代码创建一个过滤器再配置到logback里面去</p>
<pre><code class="language-java"><span class="hljs-keyword">import</span> ch.qos.logback.classic.spi.ILoggingEvent;
<span class="hljs-keyword">import</span> ch.qos.logback.core.filter.Filter;
<span class="hljs-keyword">import</span> ch.qos.logback.core.spi.FilterReply;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FilterSyncLogger</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Filter</span>&lt;<span class="hljs-title">ILoggingEvent</span>&gt; </span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> FilterReply <span class="hljs-title">decide</span><span class="hljs-params">(ILoggingEvent event)</span> </span>{

        String filterPackage = <span class="hljs-string">"com.plumelog.trace.aspect.AbstractAspect"</span>;

        <span class="hljs-keyword">if</span> (getPackName(event.getLoggerName()).equals(filterPackage)
                || getPackName(event.getLoggerName()).equals(filterPackage)) {
            <span class="hljs-keyword">return</span> FilterReply.DENY;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> FilterReply.ACCEPT;
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getPackName</span><span class="hljs-params">(String className)</span> </span>{
        <span class="hljs-keyword">return</span> className.substring(<span class="hljs-number">0</span>, className.lastIndexOf(<span class="hljs-string">"."</span>));
    }

}
</code></pre>
</div></section><footer class="footer-container"><div class="footer-body"><img src="/pic/icon_name_black.png"/><div class="cols-container"><div class="col col-12"><h3>免责声明</h3><p>本软件为免费软件并开放源码，用于交流学习，使用中遇到任何问题和纠纷本社区概不负责</p></div><div class="col col-6"><dl><dt>文档</dt><dd><a href="/zh-cn/docs/FASTSTART.html" target="_self">概览</a></dd><dd><a href="/zh-cn/docs/FASTSTART.html" target="_self">快速开始</a></dd><dd><a href="/zh-cn/docs/FASTSTARThtml" target="_self">开发者指南</a></dd></dl></div><div class="col col-6"><dl><dt>资源</dt><dd><a href="http://demo.plumelog.com" target="_self">演示地址</a></dd><dd><a href="https://gitee.com/plumeorg" target="_self">社区</a></dd></dl></div></div><div class="copyright"><span>Copyright © 2018 版权所有 plume开源社区</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>